< previous page page_100 next page >

Page 100
mouse button. Although some of the differences between the events are obviousfor example, the MouseDown event occurs when you press the mouse buttonother differences between the events do exist. In the case of pressing a mouse button, the Click event is fired only if the left mouse button is pressed; it does not respond to the click of any other mouse button. The MouseDown and MouseUp events not only respond to any mouse button, but the event also can report which button was pressed, so your program can take appropriate action.
The KeyDown, KeyPress, and KeyUp events work in a similar manner. The KeyPress event tells you only which key was pressed, not whether a Shift or Ctrl key was held down when the key was pressed. If you need that information, you need to use the KeyDown or KeyUp events.
Writing Event Procedures
With all these events going on, how do you make your code respond to them? And how do you filter out the events that you don't want? The answer to both questions is the same. To respond to any event for any object, you write program code specifically for that event happening to that object. Any object/event combination that has no code written for it is ignored. So the next question is, how do you write code for an event?
To write code, you first need to access a Code window. Do so by double-clicking a control on your form (or the form itself), by clicking the View Code button in the project window, by selecting Code from the View menu, or by pressing F7. Any of these actions presents you with the Code window for the current form (see Figure 5.3).
0100-01.jpg
Figure 5.3
Double-clicking a form or control at designtime
presents a Code window, opened to an event procedure.
Follow these steps to write a simple example:
1. Start Visual Basic and choose a Standard EXE project.
2. Double-click the form to open the Code window. By default, if no other code exists for the form, you are placed in the form's Load event procedure.
3. From the Object box, select the Form object.
4. From the Procedure box, select the MouseMove event.
note.gif Notice that when you make a selection, Visual Basic automatically sets up the skeleton of a procedure with the procedure's name and the End Sub statement (which denotes the end of the procedure).square.gif

 
< previous page page_100 next page >